{include file="public/layout" /}
<body class="bodystyle">
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
    <div class="flexigrid">
    <div class="ncap-form-default">
        <form class="form-horizontal" id="post_form" action="{:url('Discount/active_add')}" method="post">
            <dl class="row">
                <dt class="tit">
                    <label for="active_name"><em>*</em>活动名称</label>
                </dt>
                <dd class="opt">
                    <input type="text" class="input-txt" id="active_name" name="active_name" placeholder="请输入活动名称" autocomplete="off">
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="active_date"><em>*</em>活动时间</label>
                </dt>
                <dd class="opt">
                    <input type="text" class="input-txt" id="start_date" name="start_date" placeholder="2022-03-23 00:00:00" autocomplete="off" style="width: 150px !important;"> 至
                    <input type="text" class="input-txt" id="end_date" name="end_date" placeholder="2022-03-23 00:00:00" autocomplete="off" style="width: 150px !important;">
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="total_stock">选择商品 </label>
                </dt>
                <dd class="opt">
                    <a href="javascript:void(0);" onclick="choGoods();" class="ncap-btn ncap-btn-green"><i class="layui-icon">&#xe608;</i>选择限时折扣商品</a>
                    <p class="notic2">注：每个活动场次中出售的限时折扣商品，此处非必选，可在场次管理中单独设置</p>
                    <div class="none" id="goods_show_list" style="margin-top: 15px;width: 850px;">
                        <div class="flexigrid">
                            <div class="hDiv">
                                <div class="hDivBox">
                                    <table cellspacing="0" cellpadding="0" style="width: 100%;">
                                        <thead>
                                            <tr>
                                                <th abbr="id" axis="col5" class="w60">
                                                    <div class="tc">商品ID</div>
                                                </th>
                                                <th align="center" abbr="article_title" axis="col3" class="w40">
                                                    <div class="tc">封面图</div>
                                                </th>
                                                <th align="left" abbr="article_title" axis="col3" class="">
                                                    <div style="text-align: left; padding-left: 10px;" class="">商品标题</div>
                                                </th>
                                                <th abbr="article_time" axis="col6" class="w60">
                                                    <div class="tc">操作</div>
                                                </th>
                                            </tr>
                                        </thead>
                                    </table>
                                </div>
                                <div class="bDiv" style="height: auto;">
                                    <div id="flexigrid" cellpadding="0" cellspacing="0" border="0">
                                        <table style="width: 100%;">
                                            <tbody id="Template">
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="buy_type">限购次数</label>
                </dt>
                <dd class="opt">
                    <label><input type="radio" name="limit_type" value="1" checked="checked" onclick="chooseUseType(this);" />&nbsp;不限购</label>
                    <br>
                    <label><input type="radio" name="limit_type" value="2" onclick="chooseUseType(this);" />&nbsp;活动期内每人最多购买</label>
                    <input type="text" id="num_2" value="1" disabled="disabled"  class="input-txt" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');" style="width: 50px !important;">&nbsp;件
                    <br>
                    <label><input type="radio" name="limit_type" value="3" onclick="chooseUseType(this);" />&nbsp;活动期内每人每天最多购买</label>
                    <input type="text" id="num_3" value="1" disabled="disabled"  class="input-txt" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');" style="width: 50px !important;">&nbsp;件
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="preheat">活动预热</label>
                </dt>
                <dd class="opt">
                    <div class="onoff">
                        <label for="preheat1" class="cb-enable">开启</label>
                        <label for="preheat0" class="cb-disable selected">关闭</label>
                        <input id="preheat1" name="preheat" value="1" type="radio" onclick="$('#preheat_time_dl').show();" >
                        <input id="preheat0" name="preheat" value="0" type="radio" onclick="$('#preheat_time_dl').hide();" checked="checked">
                    </div>
                    <span class="err"></span>
                    <p class="notic">未达到活动开始时间的限时折扣抢购将以倒计时形式展示，此时只可浏览不可购买，未到预热时间不展示</p>
                </dd>
            </dl>
            <dl class="row none" id="preheat_time_dl">
                <dt class="tit">
                    <label for="preheat_time"><em>*</em>预热时间</label>
                </dt>
                <dd class="opt">
                    <div class="onoff">
                        <input type="text" class="input-txt" id="preheat_time" name="preheat_time" placeholder="比如：2020-05-20" autocomplete="off">
                    </div>
                    <span class="err"></span>
                    <p class="notic">预热时间不能大于开始时间</p>
                </dd>
            </dl>
            <div class="bot">
                <a href="JavaScript:void(0);" onclick="checkForm();" class="ncap-btn-big ncap-btn-green" id="submitBtn">确认提交</a>
            </div>
    </form>
    </div>
    </div>
</div>
<script>
    function chooseUseType(obj){
        var val = $(obj).val();
        if ( 1 == val ){
            $("#num_2").attr('disabled',true);
            $("#num_3").attr('disabled',true);
        }else if( 2 == val ){
            $("#num_2").attr('disabled',false);
            $("#num_3").attr('disabled',true);
        }else if( 3 == val ){
            $("#num_3").attr('disabled',false);
            $("#num_2").attr('disabled',true);
        }
    }

    var parentObj = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    function reback(info) {
        layer.closeAll();
        if (info) {
            $("#goods_show_list").show();
            info.forEach(function (v) {
                v = JSON.parse(v,true)
                if ($('div').is('.goods_show_list_div_'+v.discount_gid) == false){
                    var html = '';
                    html += '<tr class="tr goods_show_list_div goods_show_list_div_' + v.discount_gid + '">';
                    html += '    <td class="sort">';
                    html += '        <div class="w60 tc">';
                    html += '            ' + v.discount_gid;
                    html += '            <input type="hidden" name="goods[discount_gid][]" value="' + v.discount_gid + '">';
                    html += '            <input type="hidden" name="goods[aid][]" value="' + v.aid + '">';
                    html += '        </div>';
                    html += '    </td>';
                    html += '    <td class="w40">';
                    html += '        <div class="tc">';
                    html += '            <img width="40" height="40" src="' + v.litpic + '">';
                    html += '        </div>';
                    html += '    </td>';
                    html += '    <td class="" style="width: 100%;">';
                    html += '        <div class="tl" style="padding-left: 10px;">';
                    html += '            ' + v.title;
                    html += '        </div>';
                    html += '    </td>';
                    html += '    <td class="">';
                    html += '        <div class="w60 tc">';
                    html += '            <a class="btn red" href="javascript:void(0);" onclick="del(' + v.discount_gid + ');">删除</a>';
                    html += '        </div>';
                    html += '    </td>';
                    html += '</tr>';
                    $("#Template").append(html);
                }
            })
        }
    }
    function del(sid){
        $(".goods_show_list_div_"+sid).remove();
    }
    layui.use(['form', 'laydate'], function(){
        var form = layui.form,
            laydate = layui.laydate;

        laydate.render({
            elem: '#start_date'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#end_date'
            ,type: 'datetime'
        });

        laydate.render({
            elem: '#preheat_time'
            ,type: 'datetime'
        });

        form.render();
    });
    function choGoods() {
        layer.open({
            type: 2,
            title: '选择限时折扣商品',
            fixed: true, //不固定
            shadeClose: false,
            shade: 0.3,
            maxmin: true, //开启最大化最小化按钮
            area: ['80%', '90%'],
            content: "{:url('Discount/goods_list')}"
        });
    }
</script>
<script type="text/javascript">
    // 判断输入框是否为空
    function checkForm(){
        if($('input[name=active_name]').val() == ''){
            showErrorMsg('活动名称不能为空！');
            $('input[name=active_name]').focus();
            return false;
        }
        if($('input[name=start_date]').val() == ''){
            showErrorMsg('活动开始时间不能为空！');
            $('input[name=start_date]').focus();
            return false;
        }
        if($('input[name=end_date]').val() == ''){
            showErrorMsg('活动结束时间不能为空！');
            $('input[name=end_date]').focus();
            return false;
        }
        if (1 == $("input[name='preheat']:checked").val()){
            if($('input[name=preheat_time]').val() == ''){
                showErrorMsg('活动预热时间不能为空！');
                $('input[name=preheat_time]').focus();
                return false;
            }
        }

        layer_loading('正在处理');
        $.ajax({
            type : 'post',
            url : "{:url('Discount/active_add', ['_ajax'=>1])}",
            data : $('#post_form').serialize(),
            dataType : 'json',
            success : function(res){
                layer.closeAll();
                if(res.code == 1){
                    var _parent = parent;
                    _parent.layer.close(parentObj);
                    _parent.layer.msg(res.msg, {icon: 1, shade: 0.3, time: 1000}, function(){
                        _parent.window.location.reload();
                    });
                }else{
                    showErrorMsg(res.msg);
                }
            },
            error: function(e){
                layer.closeAll();
                showErrorAlert(e.responseText);
            }
        });
    }
</script>
{include file="public/footer" /}